package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Banner;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.BannerQuery;
import cn.wolfcode.luowowo.repository.BannerRepository;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
//@Transactional  暂时先别管事务
public class BannerServiceImpl implements IBannerService {
    Query query = new Query();
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private BannerRepository repository;
    @Autowired
    private MongoTemplate template;
    //目的地对象
    @Autowired
    private DestinationServiceImpl destinationService;
    @Autowired
    private IBannerService bannerService;
    
    //查询单个方法
    public Banner get(String id) {
        Optional<Banner> optional = repository.findById(id);
        Banner banner = optional.get();
        return banner;
    }
    
    //条件查询
    //page对象里面包括: 总页数 每页显示条数......
    public Page<Banner> query(BannerQuery qo) {
        /*创建一个空的条件*/
        Query query = new Query();
        /*工具类传进条件后 进行数据的排序与分页*/
        Page<Banner> query1 = DBHelper.query(template, Banner.class, query, qo.getPageable());
        //查询用户的信息
        return query1;
    }
    
    /*查询全部的内容*/
    public List<Banner> list() {
        return repository.findAll();
    }
    
    /**
     * 删除方法
     *
     * @param id 删除的id
     */
    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }
    
    /**
     * 添加与保存的方法
     *
     * @param banner
     */
    @Override
    public void saveOrUpdate(Banner banner) {
        //有部分的数据是不管添加还是编辑都是要进行添加的
        //因为保存的时候是会加上时间戳的,所以要进行分辨是新增还是编辑
        repository.save(banner);
    }
    
    /**
     * 更改游记的状态
     *
     * @param id
     * @param state
     */
    @Override
    public void changeState(String id, int state) {
        //仅仅是简单的修改状态时 就将状态设置进对象就行
        Update update = new Update();
        //如果是修改审核等的状态的话 就要进行判断,是否已经审核 不管是修改什么状态,最后的修改时间都是要进行设置的
        Banner banner = this.get(id);
        
    }
        /*修改的条件是指定的游记*/
    //获取排名前五游记
    @Override
    public List<Banner> queryTop5BySort() {
        PageRequest sequence = PageRequest.of(0, 5, Sort.Direction.ASC, "sequence");
        List<Banner> travels = repository.findAllByType(Banner.TYPE_TRAVEL, sequence);
        return travels;
    }
    //获取第一的攻略
    @Override
    public List<Banner> queryStrategyTop5BySort() {
        PageRequest sequence = PageRequest.of(0, 5, Sort.Direction.ASC, "sequence");
        List<Banner> strategys = repository.findAllByType(Banner.TYPE_STRATEGY, sequence);
        return strategys;
    }
}
